package com.jf.concurrent.queue.demo;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 主题
 * 实际为一个阻塞队列
 *
 * @author Junfeng
 */
public class Topic {

    private String name;

    private BlockingQueue queue;

    private Lock lock;

    public Topic(String name, BlockingQueue queue) {
        this.name = name;
        this.queue = queue;
        this.lock = new ReentrantLock();
    }

    public boolean offer(Object t) {
        try {
            return queue.offer(t, 1, TimeUnit.SECONDS);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }

    public Object poll() {
        try {
            return queue.poll(4, TimeUnit.SECONDS);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
}
